# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import pymysql
from doubanmovie import settings


class DoubanmoviePipeline(object):
    def __init__(self):
        self.connect = pymysql.connect(
            host=settings.MYSQL_HOST,
            db=settings.MYSQL_DBNAME,
            user=settings.MYSQL_USER,
            passwd=settings.MYSQL_PASSWD,
            charset='utf8',
            use_unicode=True)
        self.cursor = self.connect.cursor()

    def process_item(self, item, spider):
        # print(item);
        # exit();
        try:
            # self.cursor.execute(
            #     """insert into book_pool(book_name,book_cover,book_douban_id,book_evaluate,ISBN,book_author,book_press,book_producer,book_original_name,book_translator,book_press_date,book_pages,book_price,book_paperback,book_series,book_desc,author_desc,book_catalogue,book_info)
            #       value (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""",
            #     (item['book_name'],
            #      item['book_cover'],
            #      item['book_douban_id'],
            #      item['book_evaluate'],
            #      item['ISBN'],
            #      item['book_author'],
            #      item['book_press'],
            #      item['book_producer'],
            #      item['book_original_name'],
            #      item['book_translator'],
            #      item['book_press_date'],
            #      item['book_pages'],
            #      item['book_price'],
            #      item['book_paperback'],
            #      item['book_series'],
            #      item['book_desc'],
            #      item['author_desc'],
            #      item['book_catalogue'],
            #      item['book_info'],
            #      ))

            # 插入数据
            # self.cursor.execute(
            #     """insert into book_pool(book_name,book_cover,book_douban_id,book_evaluate,book_info,book_tag_name)
            #       value (%s,%s,%s,%s,%s,%s)""",
            #     (item['book_name'],
            #      item['book_cover'],
            #      item['book_douban_id'],
            #      item['book_evaluate'],
            #      item['book_info'],
            #      item['book_tag_name'],
            #      ))

            # 更新数据
            # self.cursor.execute(
            #     "update book_pool set `ISBN`=%s,`book_evaluate`=%s,`book_name`=%s,`book_author`=%s,`book_press`=%s,`book_producer`=%s,`book_original_name`=%s,`book_translator`=%s,`book_press_date`=%s,`book_pages`=%s,`book_price`=%s,`book_paperback`=%s,`book_series`=%s,`book_desc`=%s,`author_desc`=%s,`book_catalogue`=%s where book_douban_id=%s",
            #     (
            #         item['ISBN'],
            #         item['book_evaluate'],
            #         item['book_name'],
            #         item['book_author'],
            #         item['book_press'],
            #         item['book_producer'],
            #         item['book_original_name'],
            #         item['book_translator'],
            #         item['book_press_date'],
            #         item['book_pages'],
            #         item['book_price'],
            #         item['book_paperback'],
            #         item['book_series'],
            #         item['book_desc'],
            #         item['author_desc'],
            #         item['book_catalogue'],
            #         item['book_douban_id'],
            #     ))

            self.cursor.execute(
                "update book_pool set `book_evaluate`=%s,`book_name`=%s,`book_desc`=%s,`author_desc`=%s,`book_info_html`=%s where book_douban_id=%s",
                (
                    item['book_evaluate'],
                    item['book_name'],
                    item['book_desc'],
                    item['author_desc'],
                    item['book_info_html'],
                    item['book_douban_id'],
                ))
            self.connect.commit()
        except Exception as err:
            print("重复插入了==>错误信息为：" + str(err))
        return item